package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.dao.IManagerLicaishiDao;
import com.financing.platform.dto.ManagerLicaishiDTO;
import com.financing.platform.dto.ManagerLicaishiHistoryDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IManagerLicaishiService;
import com.financing.platform.vo.ManagerLicaishiVO;

@Service("managerLicaishiServiceImpl")
@Transactional
public class ManagerLicaishiServiceImpl extends BaseServiceImpl implements IManagerLicaishiService {

	@Autowired
	IManagerLicaishiDao managerLicaishiDao;
	
	@Override
	public int addManagerLicaishiUser(ManagerLicaishiDTO managerLicaishi)
			throws SQLException {
		return managerLicaishiDao.addManagerLicaishiUser(managerLicaishi);
		
	}

	@Override
	public int addManagerLicaishiUserHistory(
			ManagerLicaishiHistoryDTO managerLicaishiHistory)
			throws SQLException {
		return managerLicaishiDao.addManagerLicaishiUserHistory(managerLicaishiHistory);
	}

	@Override
	public int updateManagerLicaishiUser(ManagerLicaishiDTO managerLicaishi)
			throws SQLException {
		return managerLicaishiDao.updateManagerLicaishiUser(managerLicaishi);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int batchAddManagerLicaishiUser(ManagerLicaishiVO managerLicaishiVo,UserInfoDTO user)
			throws SQLException {
		String[] licaishiIdGroup=managerLicaishiVo.getLicaishiIds().split(",");

		List list = Arrays.asList(licaishiIdGroup);
		Set set = new HashSet(list);
		licaishiIdGroup=(String [])set.toArray(new String[0]);

		ManagerLicaishiDTO managerLicaishi=new ManagerLicaishiDTO();
		managerLicaishi.setCreateBy(user.getUsername());
		managerLicaishi.setUpdateBy(user.getUsername());
		
		for (int i = 0; i < licaishiIdGroup.length; i++) {
			if(StringUtils.isBlank(licaishiIdGroup[i]))continue;
			managerLicaishi.setFaUserId(licaishiIdGroup[i]);
			managerLicaishi.setSystemUserId(managerLicaishiVo.getSystemUserId());
			int count = addManagerLicaishiUser(managerLicaishi);
			doAddHistory(managerLicaishi);//增加历史理财师分配历史
		}
		
		return 1;
	}
	
	public int doAddHistory(ManagerLicaishiDTO managerLicaishi) throws SQLException{
		ManagerLicaishiHistoryDTO managerLicaishiHistory=new ManagerLicaishiHistoryDTO();
		managerLicaishiHistory.setFaUserId(managerLicaishi.getFaUserId());
		managerLicaishiHistory.setSystemUserIdFrom("");
		managerLicaishiHistory.setSystemUserIdTo(managerLicaishi.getSystemUserId());
		managerLicaishiHistory.setCreateBy(managerLicaishi.getCreateBy());
		managerLicaishiHistory.setCreateDate(managerLicaishi.getCreateDate());
		managerLicaishiHistory.setUpdateBy(managerLicaishi.getUpdateBy());
		managerLicaishiHistory.setUpdateDate(managerLicaishi.getUpdateDate());
		addManagerLicaishiUserHistory(managerLicaishiHistory);// ADD
		return 1;
	}

	@Override
	public int editManagerLicaishiByUser(ManagerLicaishiVO managerLicaishiVo,UserInfoDTO user) throws SQLException {
		List<ManagerLicaishiVO> list = managerLicaishiDao.queryManagerLicaishiByUser(managerLicaishiVo);
		huishouLicaishi(user, list);
		return 1;
	}

	public void huishouLicaishi(UserInfoDTO user, List<ManagerLicaishiVO> list)
			throws SQLException {
		ManagerLicaishiDTO managerLicaishi = null;
		ManagerLicaishiHistoryDTO managerLicaishiHistory = null;
		for (ManagerLicaishiVO mo : list) {
			// 理财师回收
			managerLicaishi= new ManagerLicaishiDTO();
			managerLicaishi.setId(mo.getId());
			managerLicaishi.setSystemUserId("");
			managerLicaishi.setUpdateBy(user.getUsername());
			managerLicaishi.setDeleteTag("2");
			updateManagerLicaishiUser(managerLicaishi); // UPDATE;
			// 理财师分配历史
			managerLicaishiHistory = new ManagerLicaishiHistoryDTO();
			managerLicaishiHistory.setFaUserId(mo.getFaUserId());
			managerLicaishiHistory.setSystemUserIdFrom(mo.getSystemUserId());
			managerLicaishiHistory.setSystemUserIdTo(user.getUserId());
			managerLicaishiHistory.setCreateBy(user.getUsername());
			managerLicaishiHistory.setUpdateBy(user.getUsername());
			addManagerLicaishiUserHistory(managerLicaishiHistory);// ADD
		}
	}

	@Override
	public int countManagerLicaishiByUser(ManagerLicaishiVO managerLicaishiVo)
			throws SQLException {
		return managerLicaishiDao.countManagerLicaishiByUser(managerLicaishiVo);
	}

	@Override
	public List<ManagerLicaishiVO> queryManagerLicaishiByUser(
			ManagerLicaishiVO managerLicaishiVo) throws SQLException {
		return managerLicaishiDao.queryManagerLicaishiByUser(managerLicaishiVo);
	}

	@Override
	public int huishouManagerLicaishi(List<ManagerLicaishiVO> list,
			UserInfoDTO user) throws SQLException {
		huishouLicaishi(user, list);
		return 1;
	}

	@Override
	public List<ManagerLicaishiVO> queryManagerLicaishiJob()
			throws SQLException {
		return managerLicaishiDao.queryManagerLicaishiJob();
	}
}
